﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Web.Mail;
using System.Text;

namespace Geleximco.Common {
    /// <summary>
    /// Summary description for SendMail
    /// </summary>
    public class EmailHelper {
        private String _from;
        public String From {
            get { return _from; }
            set { _from = value; }
        }

        String _passWord;
        public String PassWord {
            get { return _passWord; }
            set { _passWord = value; }
        }

        String _fromName;
        public String FromName {
            get { return _fromName; }
            set { _fromName = value; }
        }

        private String _to;
        public String To {
            get { return _to; }
            set { _to = value; }
        }

        String _toName;
        public String ToName {
            get { return _toName; }
            set { _toName = value; }
        }

        private String _subject;
        public String Subject {
            get { return _subject; }
            set { _subject = value; }
        }

        private String _body;
        public String Body {
            get { return _body; }
            set { _body = value; }
        }

        private List<String> _attachments;
        public List<String> Attachments {
            get { return _attachments; }
            set { _attachments = value; }
        }

        String _host;
        public String Host {
            get { return _host; }
            set { _host = value; }
        }

        int _port;
        public int Port {
            get { return _port; }
            set { _port = value; }
        }

        bool _sSL;
        public bool SSL {
            get { return _sSL; }
            set { _sSL = value; }
        }

        public EmailHelper() {
            From = "globalracers@gmail.com";
            PassWord = "webgroup06";
            FromName = "Gelexim Company";
            To = "";
            ToName = "";
            Subject = "";
            Body = "";
            Attachments = null;
            Host = "smtp.gmail.com";
            Port = 465;
            SSL = true;
        }

        public bool Send() {
            if (ToName.Length == 0)
                ToName = To;
            if (FromName.Length == 0)
                FromName = From;

            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = Host;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = Port.ToString();
            if (SSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (From.Length == 0) {
                //Ingen auth 
            } else {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = From;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = PassWord;
            }

            Mail.To = To;
            Mail.From = From;
            Mail.Subject = Subject;
            Mail.Body = Body;
            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;

            if (Attachments != null && Attachments.Count > 0) {
                for (int i = 0; i < Attachments.Count; i++) {
                    Mail.Attachments.Add(new MailAttachment(Attachments[i]));
                }
            }

            try {
                System.Web.Mail.SmtpMail.SmtpServer = Host;
                System.Web.Mail.SmtpMail.Send(Mail);
                return true;
            } catch (System.Exception) {
                return false;
            }
        }
    } 
}
